package weaver.formmode.customjavacode.modeexpand;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.weavernorth.humanresource.SalarRangeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;


/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 *
 * @author Administrator
 */
public class ModeExpandTemplate extends AbstractModeExpandJavaCodeNew {
    private static Log log = LogFactory.getLog(ModeExpandTemplate.class);

    /**
     * 执行模块扩展动作
     * 员工自助,基本信息提交后推送SAP
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     *              importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *              导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     * @return
     */
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        //数据库操作
        RecordSet rs = new RecordSet();
        //推送SAP 的xml拼接
        StringBuffer strRequestXml = new StringBuffer();
        //工具类
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //基础类
        BaseBean bb=new BaseBean();


        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());

                if (billid > 0 && modeid > 0) {

                    log.info("进入建模action:" + billid);
                    strRequestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:soap:functions:mc-style'>");

                    strRequestXml.append("<soapenv:Header/>");
                    strRequestXml.append("<soapenv:Body>");
                    strRequestXml.append("<urn:Zhri083>");
                    strRequestXml.append("<IBankdat></IBankdat>");
                    strRequestXml.append("<IBankl></IBankl>");
                    strRequestXml.append("<IBankn></IBankn>");
                    strRequestXml.append("<IDate></IDate>");

                    //------请在下面编写业务逻辑代码------
                    //查询保存的信息
                    String strTable = "uf_ygxxzz";
                    log.info("当前表名:" + strTable);

                    String strSql = "select uf.xm,uf.yddh,uf.hyzt,uf.xjzd,uf.jjlxr,uf.jjlxrdh,jrjtrq,jrgsrq from  " + strTable + " uf  where uf.id=?";
                    log.info("当前建模查询:" + strSql + ",billid:" + billid);
                    rs.executeQuery(strSql, billid);
                    if (rs.next()) {
                        //婚姻状态
                        String strHyzt = Util.null2String(rs.getString("hyzt"));
                        String strHyztSap = "";
                        if ("0".equals(strHyzt)) {
                            strHyztSap = "0";
                        } else if ("1".equals(strHyzt)) {
                            strHyztSap = "1";
                        } else {
                            strHyztSap = "9";
                        }
                        strRequestXml.append(" <IFamst>" + strHyztSap + "</IFamst>");
                        strRequestXml.append("<IGbort></IGbort>");
                        strRequestXml.append("<IHukot></IHukot>");

                        //员工姓名
                        String strXm = Util.null2String(rs.getString("xm"));
                        log.info("员工id:" + strXm);
                        //员工编码
                        String strWorkcode = salarRangeUtil.getWorkcode(strXm);
                        log.info("员工编码:" + strWorkcode);
                        strRequestXml.append("<IPernr>" + strWorkcode + "</IPernr>");
                        //10建模页面,20流程页面
                        strRequestXml.append("<IType>10</IType>");

                        //移动电话
                        String strYddh = Util.null2String(rs.getString("yddh"));
                        strRequestXml.append("<IUsrid>" + strYddh + "</IUsrid>");


                        //现居住地
                        String strXjzd = Util.null2String(rs.getString("xjzd"));
                        strRequestXml.append("<IZxxdz>" + strXjzd + "</IZxxdz>");


                        //紧急联系人
                        String strJjlxr = Util.null2String(rs.getString("jjlxr"));
                        //紧急联系人电话
                        String strJjlxrdh = Util.null2String(rs.getString("jjlxrdh"));
                        strRequestXml.append("<IZlxys>" + strJjlxrdh + "</IZlxys>");
                        strRequestXml.append("<IZxm>" + strJjlxr + "</IZxm>");

                        //region [2021-04-12：新增字段jrjtrq,jrgsrq]

                        //进集团日期
                        String strJtrq = Util.null2String(rs.getString("jrjtrq"));
                        //进公司日期
                        String strGsrq = Util.null2String(rs.getString("jrgsrq"));
                        strRequestXml.append("<IJtrq>" + strJtrq + "</IJtrq>");
                        strRequestXml.append("<IGsrq>" + strGsrq + "</IGsrq>");

                        //endregion


                    }


                    strRequestXml.append("<Return>");

                    strRequestXml.append("<item>");
                    strRequestXml.append("<Type></Type>");
                    strRequestXml.append("<Id></Id>");
                    strRequestXml.append("<Number></Number>");
                    strRequestXml.append("<Message></Message>");
                    strRequestXml.append(" <LogNo></LogNo>");
                    strRequestXml.append("<LogMsgNo></LogMsgNo>");
                    strRequestXml.append("<MessageV1></MessageV1>");
                    strRequestXml.append("<MessageV2></MessageV2>");
                    strRequestXml.append("<MessageV3></MessageV3>");
                    strRequestXml.append("<MessageV4></MessageV4>");
                    strRequestXml.append("<Parameter></Parameter>");
                    strRequestXml.append("<Row></Row>");
                    strRequestXml.append("<Field></Field>");
                    strRequestXml.append("<System></System>");
                    strRequestXml.append("</item>");
                    strRequestXml.append("</Return>");

                    strRequestXml.append("<T_0022>");

                    strRequestXml.append("<item>");
                    strRequestXml.append("<Slart></Slart>");
                    strRequestXml.append("<Zxlks></Zxlks>");
                    strRequestXml.append("<Zxljs></Zxljs>");
                    strRequestXml.append("<Insti></Insti>");
                    strRequestXml.append("<Insat></Insat>");
                    strRequestXml.append("<Sland></Sland>");
                    strRequestXml.append("<Anzkl></Anzkl>");
                    strRequestXml.append("<Fach3></Fach3>");
                    strRequestXml.append("<Slabs></Slabs>");
                    strRequestXml.append("<Acaqm></Acaqm>");
                    strRequestXml.append("<Acqid></Acqid>");
                    strRequestXml.append("<Acaqd></Acaqd>");
                    strRequestXml.append("<Accid></Accid>");
                    strRequestXml.append("<Acacd></Acacd>");
                    strRequestXml.append("<Acacm></Acacm>");
                    strRequestXml.append("</item>");
                    strRequestXml.append("</T_0022>");


                    strRequestXml.append("<T_9124>");
                    //查询明细表信息
                    String strDetailSql = "select *from " + strTable + "_dt1 where mainid=?";
                    log.info("获取明细信息:" + strDetailSql);
                    rs.executeQuery(strDetailSql, billid);
                    while (rs.next()) {
                        //出生日期
                        String strCsrq = Util.null2String(rs.getString("csrq"));
                        //姓名
                        String strXmChild = Util.null2String(rs.getString("xm"));
                        //性别
                        String strXb = Util.null2String(rs.getString("xb"));
                        //性别推送SAP
                        String strXbSap = "";
                        if ("0".equals(strXb)) {
                            strXbSap = "10";
                        } else if ("1".equals(strXb)) {
                            strXbSap = "20";
                        }
                        strRequestXml.append("<item>");
                        strRequestXml.append("<Zcygx></Zcygx>");
                        strRequestXml.append("<Zxm>" + strXmChild + "</Zxm>");
                        strRequestXml.append("<Zxb>" + strXbSap + "</Zxb>");
                        strRequestXml.append("<Zlxys></Zlxys>");
                        strRequestXml.append("<Zxgzfw></Zxgzfw>");
                        strRequestXml.append("<Zdate>" + strCsrq + "</Zdate>");
                        strRequestXml.append("</item>");
                    }
                    strRequestXml.append("</T_9124>");
                    strRequestXml.append("</urn:Zhri083>");
                    strRequestXml.append("</soapenv:Body>");
                    strRequestXml.append("</soapenv:Envelope>");
                    log.info("建模员工自助推送SAP请求xml:" + strRequestXml);

                    String strUrl = Util.null2String(bb.getPropValue("personnel-erdos","ygzzurl"));
                    String strResponse = salarRangeUtil.sendPost(strUrl, strRequestXml.toString(), "utf-8");
                    log.info("SAP返回结果:" + strResponse);

                    Document doc = null;//解析返回值开始
                    int intType = 0;
                    String fhz="";
                    boolean bolFlag=true;
                    String strMessage="";
                    doc = DocumentHelper.parseText(strResponse);
                    Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
                    Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                    while (its2.hasNext()) {
                        Element element = its2.next();
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        fhz = element.getText();
                        if ("E".equals(fhz)) {
                            bolFlag = false;
                            break;
                        }

                    }

                    int intMess = 0;

                    //返回值失败时,则提示失败
//                    if ("E".equals(fhz)) {
//                        Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
//                        while (its3.hasNext()) {
//                            Element element2 = its3.next();
//                            strMessage = element2.getText();
//                            log.info("intMess:" + intMess + ",intType:" + intType);
//                            if (intMess == intType) {
//                                if (!"".equals(strMessage)) {
//                                    break;
//                                }
//                            }
//
//                            intMess++;
//                        }
//                    }



                    if(!bolFlag){
                        result.put("errmsg", strMessage);
                        result.put("flag", "false");
                    }
                }
            }



        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }




}